/**
  ******************************************************************************
  * @file    r3_2_f4xx_pwm_curr_fdbk.h
  * @author  Motor Control SDK Team, ST Microelectronics
  * @brief   This file contains all definitions and functions prototypes for the
  *          r3_2_f4_pwm_curr_fdbk component of the Motor Control SDK.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  * @ingroup r3_2_f4XX_pwm_curr_fdbk
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __R3_2_PWM_CURR_FDBK_H
#define __R3_2_PWM_CURR_FDBK_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* Includes ------------------------------------------------------------------*/
#include "pwm_curr_fdbk.h"

/** @addtogroup MCSDK
  * @{
  */

/** @addtogroup pwm_curr_fdbk
  * @{
  */

/** @addtogroup r3_2_f4XX_pwm_curr_fdbk
  * @{
  */

/* Exported types ------------------------------------------------------- */
/**
  * @brief  PWMC_R3_2_Handle_t parameters structure definition
  */
typedef const struct
{
  ADC_TypeDef * ADCx_1;             /*!< It contains the pointer to the first ADC
                                        used for current reading. */
  ADC_TypeDef * ADCx_2;             /*!< It contains the pointer to the second ADC
                                        used for current reading. */
  TIM_TypeDef * TIMx;                   /*!< It contains the pointer to the timer
                                            used for PWM generation. It must
                                            equal to TIM1 if M1, to TIM8 otherwise */
  GPIO_TypeDef * pwm_en_u_port;     /*!< phase u enable driver signal GPIO port */
  GPIO_TypeDef * pwm_en_v_port;     /*!< phase v enable driver signal GPIO port */
  GPIO_TypeDef * pwm_en_w_port;     /*!< phase w enable driver signal GPIO port */
  uint32_t      pwm_en_u_pin;       /*!< phase u enable driver signal pin */
  uint32_t      pwm_en_v_pin;       /*!< phase v enable driver signal pin */
  uint32_t      pwm_en_w_pin;       /*!< phase w enable driver signal pin */

  volatile uint32_t  *ADCDataReg1[6];
  volatile uint32_t  *ADCDataReg2[6];
  uint32_t ADCConfig1[6];
  uint32_t ADCConfig2[6];

  uint16_t Tw;                    /*!< It is used for switching the context
                                       in dual MC. It contains biggest delay
                                       (expressed in counter ticks) between
                                       the counter crest and ADC latest trigger
                                       */
  uint16_t hTafter;                    /*!< It is the sum of dead time plus max
                                            value between rise time and noise time
                                            express in number of TIM clocks.*/
  uint16_t hTbefore;                   /*!< It is the sampling time express in
                                            number of TIM clocks.*/
  uint16_t hDeadTime;                  /*!< Dead time in number of TIM clock
                                            cycles. If CHxN are enabled, it must
                                            contain the dead time to be generated
                                            by the microcontroller, otherwise it
                                            expresses the maximum dead time
                                            generated by driving network */
  uint8_t  bFreqRatio;             /*!< It is used in case of dual MC to
                                        synchronize TIM1 and TIM8. It has
                                        effect only on the second instanced
                                        object and must be equal to the
                                        ratio between the two PWM frequencies
                                        (higher/lower). Supported values are
                                        1, 2 or 3 */
  uint8_t  bIsHigherFreqTim;       /*!< When bFreqRatio is greather than 1
                                        this param is used to indicate if this
                                        instance is the one with the highest
                                        frequency. Allowed value are: HIGHER_FREQ
                                        or LOWER_FREQ */
  uint8_t  RepetitionCounter;         /*!< It expresses the number of PWM
                                            periods to be elapsed before compare
                                            registers are updated again. In
                                            particular:
                                            RepetitionCounter= (2* #PWM periods)-1*/
  LowSideOutputsFunction_t LowSideOutputs; /*!< Low side or enabling signals
                                                generation method are defined
                                                here.*/
  FunctionalState EmergencyStop;        /*!< It enable/disable the management of
                                           an emergency input instantaneously
                                           stopping PWM generation. It must be
                                           either equal to ENABLE or DISABLE */
} R3_2_Params_t;
  
typedef struct
{
  PWMC_Handle_t _Super;     /*!< base component handler   */
  uint32_t PhaseAOffset;   /*!< Offset of Phase A current sensing network  */
  uint32_t PhaseBOffset;   /*!< Offset of Phase B current sensing network  */
  uint32_t PhaseCOffset;   /*!< Offset of Phase C current sensing network  */
  uint32_t ADC_ExternalTriggerInjected; /*!<  external  trigger selection */
  uint32_t ADCTriggerEdge;  /*!<  trigger edge selection */
  uint16_t Half_PWMPeriod;  /* Half PWM Period in timer clock counts */
  uint8_t  CalibSector;       /*!< the space vector sector number during calibration */
  volatile uint8_t PolarizationCounter;
  bool OverCurrentFlag;     /*!< This flag is set when an overcurrent occurs.*/
  R3_2_Params_t const *pParams_str;
} PWMC_R3_2_Handle_t;


/* Exported functions ------------------------------------------------------- */

/* Initializes a PWMC_R3_2 component */
void R3_2_Init( PWMC_R3_2_Handle_t * pHandle );

/* Calibrates current reading for a PWMC_R3_2 component */
void R3_2_CurrentReadingCalibration( PWMC_Handle_t * pHandle );

/* Returns the last phase currents values measured*/
void R3_2_GetPhaseCurrents( PWMC_Handle_t * pHandle,ab_t* pStator_Currents);

/* Turns the low side switches on */
void R3_2_TurnOnLowSides( PWMC_Handle_t * pHandle );

/* Switches on PWM generation */
void R3_2_SwitchOnPWM( PWMC_Handle_t * pHandle );

/* Switches off PWM generation */
void R3_2_SwitchOffPWM( PWMC_Handle_t * pHandle );

/* Sets the ADC sampling point */
uint16_t R3_2_SetADCSampPointCalibration( PWMC_Handle_t * pHandle );

/* Sets the ADC sampling point */
uint16_t R3_2_SetADCSampPointSectX( PWMC_Handle_t * pHandle );

/* Handles the Timer Interrupts of a PWMC_R3 component */
void *R3_2_TIMx_UP_IRQHandler( PWMC_R3_2_Handle_t * pHandle);

/* It contains the Break event interrupt */
void *R3_2_BRK_IRQHandler(PWMC_R3_2_Handle_t *pHdl);

/* Returns whether an over current condition has occurred */
uint16_t R3_2_IsOverCurrentOccurred( PWMC_Handle_t * pHandle );

/**
  * It is used to enable the PWM mode during RL Detection Mode.
  */
void R3_2_RLDetectionModeEnable( PWMC_Handle_t * pHandle  );

/**
  * It is used to disable the PWM mode during RL Detection Mode.
  */
void R3_2_RLDetectionModeDisable( PWMC_Handle_t * pHandle  );

/**
  * It is used to set the PWM dutycycle during RL Detection Mode.
  */
uint16_t R3_2_RLDetectionModeSetDuty( PWMC_Handle_t * pHandle , uint16_t hDuty );


/* Returns whether an over current condition has occurred */
void RLTurnOnLowSidesAndStart( PWMC_Handle_t * pHandle  );


/**
 * @brief  It sets ADC sampling points.
 *         This function is specific for MP phase.
 */
void RLSetADCSampPoint( PWMC_Handle_t * pHandle  );

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif /* __cpluplus */

#endif /*__R3_2_PWM_CURR_FDBK_H*/

/******************* (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/
